package com.zmn.mcc.services.impl.menu;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.model.entity.menu.McMenu;
import com.zmn.mcc.model.entity.menu.McMenuQuery;
import com.zmn.mcc.persistence.interfaces.menu.MenuDao;
import com.zmn.mcc.services.interfaces.menu.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 描述: 菜单配置表接口实现类
 *
 * @author guoqiao
 * @since 2021-05-28 16:23:56
 */
@Slf4j
@Service
public class MenuServiceImpl implements MenuService {

    private static final String TAG = "菜单配置表基础服务接口";

    @Resource
    private MenuDao menuDao;

    @Override
    public McMenu findById(Integer mcMenuId) {

        if (Objects.isNull(mcMenuId)) {
            log.error("[{}],根据id查询数据mcMenuId为null", TAG);
            return null;
        }
        return menuDao.findById(mcMenuId);
    }

    @Override
    public List<McMenu> listPageByQuery(McMenuQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        query.setTotalCount(menuDao.countByQuery(query));
        return menuDao.listPageByQuery(query);
    }

    @Override
    public List<McMenu> listByQuery(McMenuQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return menuDao.listByQuery(query);
    }

    @Override
    public List<McMenu> findByParentIdAndStatus(Integer parentId, Integer status) {
        if (Objects.isNull(parentId) || Objects.isNull(status)) {
            log.error("[{}],根据parentId和Status查询数据，parentId:{},status:{}", TAG, parentId, status);
            return null;
        }
        return menuDao.findByParentIdAndStatus(parentId, status);

    }

    @Override
    public Integer countByQuery(McMenuQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return menuDao.countByQuery(query);
    }

    @Override
    public Integer updateByKey(McMenu mcMenu) {

        if (Objects.isNull(mcMenu.getMenuId())) {
            log.error("[{}],根据id修改数据,McMenu为null", TAG);
            return null;
        }
        return menuDao.updateByKey(mcMenu);
    }

    @Override
    public void updateStatusByPermitUrls(List<String> permitUrls, Integer status, String updater) {

        if (CollectionUtils.isEmpty(permitUrls) || NumberUtil.isNullOrZero(status)) {
            return;
        }
        menuDao.updateStatusByPermitUrls(permitUrls, status, updater);
    }

    @Override
    public Integer insert(McMenu mcMenu) {

        if (Objects.isNull(mcMenu)) {
            log.error("[{}],新增数据,mcMenu为null", TAG);
            return null;
        }
        return menuDao.insert(mcMenu);
    }

    @Override
    public Integer deleteByKey(Integer mcMenuId) {

        if (Objects.isNull(mcMenuId)) {
            log.error("[{}],根据id删除数据,McMenuId为null", TAG);
            return null;
        }
        return menuDao.deleteByKey(mcMenuId);
    }

}